using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.SaleReturn
{
    public partial class guiSelectPOS : THSERPScreen
    {
        public guiSelectPOS()
        {
            InitializeComponent();
        }

        private void guiSelectPOS_Load(object sender, EventArgs e)
        {
            fld_dteSearchFromARInvoiceDate.DateTime = THSUtil.GetYearBeginDate();
            fld_dteSearchToARInvoiceDate.DateTime = THSUtil.GetYearEndDate();
        }

        private void fld_btnOK_Click(object sender, EventArgs e)
        {
            if (SelectPOS())
                this.DialogResult = DialogResult.OK;
            else
                this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void fld_dgcBRPOSs_DoubleClick(object sender, EventArgs e)
        {
            if (SelectPOS())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public bool SelectPOS()
        {
            if (fld_dgvARInvoices.FocusedRowHandle >= 0)
            {
                DataRow row = fld_dgvARInvoices.GetDataRow(fld_dgvARInvoices.FocusedRowHandle);

                //Generate sale return from invoice
                ARInvoicesController objInvoicesController = new ARInvoicesController();
                ARInvoicesInfo objInvoicesInfo = (ARInvoicesInfo)objInvoicesController.GetObjectFromDataRow(row);
                SaleReturnEntities entity = (SaleReturnEntities)((BaseModuleERP)Module).CurrentModuleEntity;
                ARSaleReturnsInfo objSaleReturnsInfo = (ARSaleReturnsInfo)entity.MainObject;
                objSaleReturnsInfo.FK_ARCustomerID = objInvoicesInfo.FK_ARCustomerID;
                objSaleReturnsInfo.FK_ARPriceLevelID = objInvoicesInfo.FK_ARPriceLevelID;
                objSaleReturnsInfo.ARSaleReturnReference = objInvoicesInfo.ARInvoiceReference;
                objSaleReturnsInfo.ARSaleReturnDiscountPerCent = objInvoicesInfo.ARInvoiceDiscountPerCent;
                objSaleReturnsInfo.ARSaleReturnDiscountFix = objInvoicesInfo.ARInvoiceDiscountFix;
                entity.UpdateMainObjectBindingSource();

                GenerateReferenceInvoiceItems(objInvoicesInfo.ARInvoiceID);

                //Copy the invoice's payment to the sale return's one
                AROpenDocumentsInfo objOpenDocumentsInfo = (AROpenDocumentsInfo)new AROpenDocumentsController().GetFirstObjectByForeignColumn("FK_ARInvoiceID", objInvoicesInfo.ARInvoiceID);
                DataSet ds = new ARDocumentPaymentsController().GetAllDataByForeignColumn("FK_AROpenDocumentID", objOpenDocumentsInfo.AROpenDocumentID);
                THSList<ARDocumentPaymentsInfo> lstDocumentPayments = new THSList<ARDocumentPaymentsInfo>(null, String.Empty, THSUtil.GetTableNameFromBusinessObjectType(typeof(ARDocumentPaymentsInfo)));
                lstDocumentPayments.Invalidate(ds);
                foreach (ARDocumentPaymentsInfo objDocumentPaymentsInfo in lstDocumentPayments)
                {
                    ds = new ARDocumentPaymentDetailsController().GetAllDataByForeignColumn("FK_ARDocumentPaymentID", objDocumentPaymentsInfo.ARDocumentPaymentID);
                    THSList<ARDocumentPaymentDetailsInfo> lstDocumentPaymentDetails = new THSList<ARDocumentPaymentDetailsInfo>(null, String.Empty,
                                                                                                THSUtil.GetTableNameFromBusinessObjectType(typeof(ARDocumentPaymentDetailsInfo)));
                    lstDocumentPaymentDetails.Invalidate(ds);
                    foreach (ARDocumentPaymentDetailsInfo objDocumentPaymentDetailsInfo in lstDocumentPaymentDetails)
                    {
                        int pos = lstDocumentPaymentDetails.PosOf("ARPaymentMethodCombo", objDocumentPaymentDetailsInfo.ARPaymentMethodCombo);
                        if (pos >= 0)
                        {
                            entity.ARDocumentPaymentDetailsList[pos].ARDocumentPaymentDetailAmount += objDocumentPaymentDetailsInfo.ARDocumentPaymentDetailAmount;
                            entity.ARDocumentPaymentDetailsList[pos].ARDocumentPaymentDetailChangeAmount += objDocumentPaymentDetailsInfo.ARDocumentPaymentDetailChangeAmount;
                        }
                    }
                }
                entity.ARDocumentPaymentDetailsList.GridControl.RefreshDataSource();
                return true;
            }
            return false;
        }

        public void GenerateReferenceInvoiceItems(int invoiceID)
        {
            SaleReturnEntities entity = (SaleReturnEntities)((BaseModuleERP)Module).CurrentModuleEntity;
            ARInvoiceItemsController objInvoiceItemsController = new ARInvoiceItemsController();
            DataSet ds = objInvoiceItemsController.GetAllDataByForeignColumn("FK_ARInvoiceID", invoiceID);
            entity.ARReferenceInvoiceItemsList.Invalidate(ds);
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void guiSelectPOS_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK)
                ((BaseModuleERP)Module).ActionCancel();                
        }

        private void fld_btnSearch_Click(object sender, EventArgs e)
        {
            
            String strQuery = GenerateSearchQuery(THSUtil.GetTableNameFromBusinessObjectType(typeof(ARInvoicesInfo)));
            strQuery += String.Format(" AND (ARInvoiceTypeCombo = '{0}' OR ARInvoiceTypeCombo = '{1}')", ADConfigValueUtility.cstOpenDocumentTypeSaleReceipt, ADConfigValueUtility.cstOpenDocumentTypeLayBy);
            strQuery += String.Format(" AND EXISTS (SELECT COUNT(*) FROM ARInvoiceItems WHERE ARInvoiceItems.AAStatus = 'Alive' AND FK_ARInvoiceID = ARInvoiceID HAVING COUNT(*) > 0)");
            DataSet ds = THSDbUtil.ExecuteQuery(strQuery);
            if (ds.Tables.Count > 0)
                fld_dgcARInvoices.DataSource = ds.Tables[0];
        }
    }
}